package com.hzdl.cinema.Service.impl;

import com.hzdl.cinema.Service.ScheduleService;
import com.hzdl.cinema.dao.PlayDao;
import com.hzdl.cinema.dao.RepertoireDao;
import com.hzdl.cinema.dao.ScheduleDao;
import com.hzdl.cinema.dao.StudioDao;
import com.hzdl.cinema.dao.impl.PlayDaoImpl;
import com.hzdl.cinema.dao.impl.RepertoireDaoImpl;
import com.hzdl.cinema.dao.impl.ScheduleDaoImpl;
import com.hzdl.cinema.dao.impl.StudioDaoImpl;
import com.hzdl.cinema.domain.Play;
import com.hzdl.cinema.domain.Schedule;
import com.hzdl.cinema.domain.Studio;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class ScheduleServiceImpl implements ScheduleService {
    ScheduleDao scheduleDao = new ScheduleDaoImpl();
    RepertoireDao repertoireDao = new RepertoireDaoImpl();

    PlayDao playDao = new PlayDaoImpl();
    StudioDao studioDao = new StudioDaoImpl();


    //详情页面上的影片是否开演
    @Override
    public Schedule schedule(String playName) {
        Schedule schedule = new Schedule();
        //演出计划scheduleId
        //影片playId
        // 演播厅studioId
        // 开始时间startTime
        // 价钱schedulePrice
        schedule = scheduleDao.schedule(playName);
        if (schedule == null) {
            return null;
        } else {
            // 影片play
            Play play = scheduleDao.findPlay(playName);
            schedule.setPlay(play);
            // 演播厅studio
            Studio studio = scheduleDao.findStudio(schedule.getStudioId());
            schedule.setStudio(studio);

            return schedule;
        }
    }


    @Override
    public List<Schedule> seeSchedule() {
        List<Schedule> list = scheduleDao.seeSchedule();
//      Play play=scheduleDao.seePlay(schedule.getPlayId())
        return list;
    }

    @Override
    public Play seePlay(String playId) {
        Play play = repertoireDao.playEcho(playId);
        return play;
    }

    @Override
    public Studio seeStudio(String studioId) {
        Studio studio = scheduleDao.seeStudio(studioId);
        return studio;
    }

    @Override
    public List<Schedule> findAllSchedule() {
        List<Schedule> scheduleList = scheduleDao.findAllSchedule();
        for (Schedule schedule : scheduleList) {
            Play play = playDao.findMyPlay(schedule.getPlayId());
            Studio studio = studioDao.findMyStudio(schedule.getStudioId());
            schedule.setPlay(play);
            schedule.setStudio(studio);
        }
        return scheduleList;
    }

    @Override
    public Schedule findScheduleById(int scheduleId) {
        Schedule schedule = scheduleDao.findScheduleById(scheduleId);
        return schedule;
    }

    @Override
    public void modifyScheduleById(Schedule schedule) {
        scheduleDao.modifyScheduleById(schedule.getScheduleId(), schedule.getStartTime(), schedule.getSchedulePrice());
        playDao.modifyPlayName(schedule.getScheduleId(), schedule.getPlay().getPlayName());
        studioDao.modifyStudioName(schedule.getScheduleId(), schedule.getStudio().getStudioName());
    }

    @Override
    public void deleteScheduleById(int scheduleId) {
        scheduleDao.deleteScheduleById(scheduleId);
    }

    @Override
    public void addSchedule(String playName, String studioName, Date startTime, BigDecimal schedulePrice) {
        int playId = playDao.findIdByName(playName);
        int studioId = studioDao.findIdByName(studioName);
        scheduleDao.addSchedule(playId, studioId, startTime, schedulePrice);
    }

    @Override
    public Integer searchPlayId(String playName) {
//        scheduleDao.searchSchedule()
        int playId = playDao.findIdByName(playName);
        return playId;
    }

    @Override
    public Schedule searchSchedule(int playId) {
        Schedule schedule = scheduleDao.searchSchedule(playId);
        if (schedule == null) {
            return schedule;
        } else {
            Studio studio = studioDao.findStudioById(schedule.getStudioId());
            schedule.setStudio(studio);
            return schedule;
        }
    }

}
